package kr.hkit.password.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDatabaseManager extends SQLiteOpenHelper {
	static final String DATABASE_NAME = "Info.db";
	static final String TABLE_NAME_A = "account";
	static final String TABLE_NAME_L = "item";
	static final int DATABASE_VER = 11;
	public final String[] itemColumns = { "id", "password", "url", "name",
			"cycle", "date", "account" };
	public final String[] arCycle = { "10초", "1일", "3일", "7일", "15일", "30일" };
	private static final String TAG = "PW";

	Context mContext;

	private static MyDatabaseManager mDBManager;

	public MyDatabaseManager(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		mContext = context;
	}

	public MyDatabaseManager(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VER);
	}

	public static MyDatabaseManager getInstance(Context context) {
		if (mDBManager == null) {
			mDBManager = new MyDatabaseManager(context, DATABASE_NAME, null,
					DATABASE_VER);
		}
		return mDBManager;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "AccountDBM onCreate");
		db.execSQL("CREATE TABLE IF NOT EXISTS account("
				+ "account TEXT PRIMARY KEY REFERENCES item(account) on DELETE CASCADE NOT NULL,"
				+ "password TEXT NOT NULL,"
				+ "hint INTEGER NOT NULL, "
				+ "answer TEXT NOT NULL, "
				+ "alarm INTEGER DEFAULT 0);");

		db.execSQL("CREATE TABLE IF NOT EXISTS item("
				+ "id TEXT NOT NULL, "
				+ "password TEXT NOT NULL, "
				+ "url TEXT NOT NULL, "
				+ "name TEXT NOT NULL, "
				+ "cycle TEXT NOT NULL, "
				+ "date TEXT NOT NULL, "
				+ "account TEXT NOT NULL);");

		db.execSQL("insert into account values('test', 'test', 0, 'test', 0);");

		db.execSQL("insert into item values('naver', 'naver', 'http://www.naver.com', '네이버', '0', '2015-01-22-01', 'test');");
		db.execSQL("insert into item values('google', 'google', 'http://www.google.co.kr', '구글', '1', '2015-01-15-02', 'test');");

		db.execSQL("PRAGMA foreign_keys = on;");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "ItemDBM onUpgrade");
		db.execSQL("DROP TABLE IF EXISTS account");
		db.execSQL("DROP TABLE IF EXISTS item");
		onCreate(db);
	}

	public Cursor select(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		Log.d(TAG, "AccountDBM select");
		if (table == "account") {
			return getReadableDatabase().query(TABLE_NAME_A, columns,
					selection, selectionArgs, groupBy, having, orderBy);
		} else {
			return getReadableDatabase().query(TABLE_NAME_L, columns,
					selection, selectionArgs, groupBy, having, orderBy);
		}
	}

	public long update(String table, ContentValues addRowValue,
			String whereClause, String[] whereArgs) {
		if (table == "account") {
			return getWritableDatabase().update(TABLE_NAME_A, addRowValue,
					whereClause, whereArgs);
		} else {
			return getWritableDatabase().update(TABLE_NAME_L, addRowValue,
					whereClause, whereArgs);
		}
	}

	public long insert(String table, ContentValues addRowValue) {
		if (table == "account") {
			return getWritableDatabase()
					.insert(TABLE_NAME_A, null, addRowValue);
		} else {
			return getWritableDatabase()
					.insert(TABLE_NAME_L, null, addRowValue);
		}
	}

}
